//
//  CommenTools.m
//  Baymax
//
//  Created by jolin on 16/4/19.
//  Copyright © 2016年 jolin. All rights reserved.
//

#import "CommenTools.h"
#import <objc/runtime.h>
#import "MBProgressHUD.h"
#import "DiaryViewController.h"
#import "DiscoverViewController.h"
#import "PersonViewController.h"
#import "RootViewController.h"

@implementation CommenTools

#pragma mark - 校验手机号
+ (BOOL) validateMobile:(NSString *)mobile {
    NSString *phoneRegex = @"/^1[3|4|5|7|8][0-9]\\d{8}$/";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

#pragma mark - 将模型类转化为字典类型
+ (NSDictionary *)getDicWithModel:(id)model {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([model class], &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        const char* char_f =property_getName(property);
        NSString *propertyName = [NSString stringWithUTF8String:char_f];
        id propertyValue = [model valueForKey:(NSString *)propertyName];
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
    } 
    free(properties); 
    return props;
}

#pragma mark - 弹出文本提示框
+ (void)alertPromptWithView:(UIView *) view prompt:(NSString *)prompt {
    __block MBProgressHUD *promptView = [[MBProgressHUD alloc]initWithView:view];
    [view addSubview:promptView];
    promptView.labelText = prompt;
    promptView.mode = MBProgressHUDModeText;
    
    [promptView showAnimated:YES whileExecutingBlock:^{
        sleep(2);
    } completionBlock:^{
        [promptView removeFromSuperview];
        //[promptView release];
        promptView = nil;
    }];
  
}

#pragma mark - 设置tabBar
+ (UITabBarController *)setUpTabBar {
    UITabBarController *tabBarVC = [[UITabBarController alloc]init];
    RootViewController *rootVC = [[RootViewController alloc]init];
    rootVC.title = @"心情";
    DiaryViewController *diaryVC = [[DiaryViewController alloc]init];
    diaryVC.title = @"日记";
    DiscoverViewController *discoverVC = [[DiscoverViewController alloc]init];
    discoverVC.title = @"发现";
    PersonViewController *personVC = [[PersonViewController alloc]init];
    personVC.title = @"我";
    
    UINavigationController *navVC_root = [[UINavigationController alloc]initWithRootViewController:rootVC];
    UINavigationController *navVC_diary = [[UINavigationController alloc]initWithRootViewController:diaryVC];
    UINavigationController *navVC_discover = [[UINavigationController alloc]initWithRootViewController:discoverVC];
    UINavigationController *navVC_person = [[UINavigationController alloc]initWithRootViewController:personVC];
    
    tabBarVC.viewControllers = @[navVC_root, navVC_diary, navVC_discover, navVC_person];
    
    return tabBarVC;
}
@end
